package com.zhaotianhao.utils;

import java.sql.Connection;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadInsertUtils<T> {
    /*public static void exec(List<String[]> list,String sql) throws Exception{
        int count = 10000;                 //一个线程处理10000条数据
        int listSize = list.size();        //数据集合大小
        int runSize = (listSize/count)+1;  //开启的线程数
        List<String[]> newlist = null;     //存放每个线程的执行数据
        String addSql =sql;  //SQL
        Connection connection = JDBCUtil.getConn(); //连接数据库
        ExecutorService executor = Executors.newFixedThreadPool(runSize);      //创建一个线程池，数量和开启线程的数量一样
        //创建两个个计数器
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(runSize);
        //循环创建线程
        for (int i = 0; i < runSize ; i++) {
            //计算每个线程执行的数据
            if((i+1)==runSize){
                int startIndex = (i*count);
                int endIndex = list.size();
                newlist= list.subList(startIndex, endIndex);
            }else{
                int startIndex = (i*count);
                int endIndex = (i+1)*count;
                newlist= list.subList(startIndex, endIndex);//0-5000  5000-9999  10000-14999
            }
            //线程类
            MyThread mythead = new MyThread(newlist,begin,end,connection,addSql);
            //这里执行线程的方式是调用线程池里的executor.execute(mythead)方法。
            executor.execute(mythead);
        }
        begin.countDown();
        end.await();
        connection.close();
        //执行完关闭线程池
        executor.shutdown();
    }
*/


    public static<T> void execObject(List<T> list, String sql) throws Exception{
        int count = 20000;                 //一个线程处理10000条数据
        int listSize = list.size();        //数据集合大小
        int runSize = (listSize/count)+1;  //开启的线程数
        List<T> newlist = null;     //存放每个线程的执行数据
        String addSql =sql;  //SQL
        Connection connection = com.zhaotianhao.utils.JDBCUtil.getConn(); //连接数据库
        ExecutorService executor = Executors.newFixedThreadPool(runSize);      //创建一个线程池，数量和开启线程的数量一样
        //创建两个个计数器
        CountDownLatch begin = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(runSize);
        //循环创建线程
        for (int i = 0; i < runSize ; i++) {
            //计算每个线程执行的数据
            if((i+1)==runSize){
                int startIndex = (i*count);
                int endIndex = list.size();
                newlist= list.subList(startIndex, endIndex);
            }else{
                int startIndex = (i*count);
                int endIndex = (i+1)*count;
                newlist= list.subList(startIndex, endIndex);//0-5000  5000-9999  10000-14999
            }
            //线程类
            com.zhaotianhao.utils.MyThread mythead = new com.zhaotianhao.utils.MyThread(newlist,begin,end,connection,addSql);
            //这里执行线程的方式是调用线程池里的executor.execute(mythead)方法。
            executor.execute(mythead);
        }
        begin.countDown();
        end.await();
        connection.close();
        //执行完关闭线程池
        executor.shutdown();
    }

}
